package com.xmg.design23.d11_proxy.cglib_proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @description: //TODO 添加类描述信息
 * @Author: mazhongqing
 * @Date: 2021/3/30 18:27
 */
public class ProxyFactory implements MethodInterceptor {
    TrainStation station = new TrainStation();
    public TrainStation getProxyObject(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(TrainStation.class);
        enhancer.setCallback(this);
        TrainStation proxy = (TrainStation) enhancer.create();
        return proxy;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代理方法被调用了");
        Object obj = method.invoke(station, objects);
        return obj;
    }
}
